package com.neusoft.neuiotms.auth.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.auth.dto.AuthUserDTO;
import com.neusoft.bizcore.auth.model.AuthOganization;
import com.neusoft.bizcore.auth.repository.AuthOganizationRepository;
import com.neusoft.bizcore.auth.service.AuthUserService;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.auth.dto.AuthOrgUserDTO;
import com.neusoft.neuiotms.common.constant.AHConstants;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("组织管理")
@RestController
@RequestMapping(value = "/api/w/auth/organizations")
public class AuthOganizationExController {
    @Autowired
    private AuthOganizationRepository authOganizationRepository;
    @Autowired
    private AuthUserService authUserService;

    @ApiOperation("根据用户组织属性，查询可用部门列表")
    @GetMapping("/ex")
    public ResultListDTO<AuthOrgUserDTO> search(final UserBean userBean) {
        final List<AuthOrgUserDTO> results = Lists.newArrayList();
        if (userBean.getOrgs().size() > 0) {
            final String orgCode = userBean.getOrgs().get(0);
            final List<AuthOganization> all = this.authOganizationRepository.findAll();
            // 科技公司部门的code都是以kj开头
            if (orgCode.indexOf(AHConstants.BELONG_KJ_CODE) > -1) {
                final List<AuthOganization> availableOrgModels =
                        all.stream().filter(it -> it.getCode().indexOf(AHConstants.BELONG_KJ_CODE) > -1)
                                .collect(Collectors.toList());

                results.addAll(availableOrgModels.stream().map(it -> this.convert(it)).collect(Collectors.toList()));

            } else {
                final List<AuthOganization> availableOrgModels =
                        all.stream().filter(it -> "root".equals(it.getCode())
                                || "root".equals(it.getParent().getCode()))
                                .collect(Collectors.toList());
                results.addAll(availableOrgModels.stream().map(it -> this.convert(it)).collect(Collectors.toList()));
            }
        }

        results.forEach(it -> {
            final Searchable searchable = new Searchable();
            searchable.put("orgCode", it.getCode());
            final List<AuthUserDTO> users = this.authUserService.findAll(searchable);
            it.getUsers().addAll(users.stream().map(user -> this.convert(user)).collect(Collectors.toList()));
        });

        return ResultListDTO.success(results);
    }

    private AuthOrgUserDTO convert(AuthOganization org) {
        final AuthOrgUserDTO dto = new AuthOrgUserDTO();
        dto.setCode(org.getCode());
        dto.setName(org.getName());
        return dto;
    }

    private Map<String, String> convert(AuthUserDTO user) {
        final Map<String, String> map = Maps.newHashMap();
        map.put("username", user.getUsername());
        map.put("name", user.getName());
        return map;
    }

}
